var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp()
Page({
  data: {
    goodsList: [],
    name: '',
    a: 1,
    latitude:'',
    longitude: '',
    page: 1,
    size:10,
    totalPages:1,
    changeFlag: false //搜索框是否改动过，改动为true，可以重新下拉，false则不执行重新下拉
  },
  //事件处理函数
  closeSearch: function () {
    wx.navigateBack()
  },
  onLoad: function () {

  },
  onShow: function () {
    let that= this
    wx.getLocation({
      type: 'wgs84',
      success(res) {
        that.setData({
          latitude: res.latitude,
          longitude: res.longitude
        })
      }
    })
  },
  inputChange: function (e) {
    this.setData({
      name: e.detail.value,
    });
  },

  getGoodsList: function () {
    let that = this;
    console.log(that.data.latitude)
    console.log(that.data.longitude)
    if (that.data.name.replace(/\s*/g, "") == null || that.data.name.replace(/\s*/g, "") == ''){
      wx.showToast({
        title: '请输入机构名称',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    util.request(api.SearchMechanism, {
      name: that.data.name.replace(/\s*/g, ""),
      latitude: that.data.latitude,
      longitude: that.data.longitude,
      page: that.data.page,
      size: that.data.size
    }).then(function(res) {
      if (res.errno === 0) {
        if(res.data.list.length == 0){
          that.setData({
            goodsList: res.data.list,
            a: 0
          })
        }else{
          that.setData({
            goodsList: res.data.list,
            a :1
          });
        }
      }
    });
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onKeywordConfirm(event) {
    this.setData({
      changeFlag:true,
      page:1
    })
    this.getGoodsList();
  },
  search(){

  },
  onReachBottom() {
    let that = this;
    if(!that.data.changeFlag){
      wx.showToast({
        title: '没有更多机构了!',
        icon: 'none',
        duration: 2000
      });
      return false;
    }

    if (that.data.name.replace(/\s*/g, "") == null || that.data.name.replace(/\s*/g, "") == '') {
      wx.showToast({
        title: '请输入机构名称',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    let pages = that.data.page +1;
    util.request(api.SearchMechanism, {
      name: that.data.name.replace(/\s*/g, ""),
      latitude: that.data.latitude,
      longitude: that.data.longitude,
      page: pages,
      size: that.data.size
    }).then(function (res) {
      if (res.errno === 0) {
        if (res.data.list.length == 0) {
          that.setData({
            changeFlag:false
          })
          wx.showToast({
            title: '没有更多机构了',
            icon: 'none',
            duration: 2000
          });
          return false;
        } else {
          let f1 = that.data.goodsList;
          let f2 = res.data.list;
          for (let i = 0; i < f2.length; i++) {
            let last = f1.length - 1;
            if (last >= 0 && f1[last].name === f2[i].name) {
              f1[last].push(f2[i]);
            }
            else {
              f1.push(f2[i]);
            }
          }
          console.log(f1)
          that.setData({
            goodsList: f1,
            page: pages,
            a: 1
          });
        }
      }
    });

  }
})